package com.wman.a17_thread_interaction;

/**
 * ThreadLocal 针对
 */
public class CustomizableThreadDemo implements TestDemo {
    private CustomizableThread mThread = new CustomizableThread();
    private ThreadLocal<String> name;

    @Override
    public void runTest() {
        mThread.start();
        try {
            Thread.sleep(1_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        mThread.looper.setTask(new Runnable() {
            @Override
            public void run() {
                System.out.println("sssssssssssssssssssssssss");
            }
        });
        try {
            Thread.sleep(1_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        mThread.looper.loop();
        mThread.looper.quit();


        //===================================
        name.get();
        name.set("wman");
    }

    class CustomizableThread extends Thread {
        private Looper looper = new Looper();
    }

    class Looper {
        private Runnable task;
        private boolean quit;

        private synchronized void setTask(Runnable runnable) {
            this.task = runnable;
        }

        private synchronized void quit() {
            quit = true;
        }

        public void loop() {
            while (!quit) {
                synchronized (this) {
                    if (task != null) {
                        task.run();
                        task = null;
                    }
                }
            }
        }
    }

    class Handler {

    }
}
